package com.yiren.manager.ctrl.pension;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.StringUtil;
import com.yiren.core.repository.utils.QueryResult;
import com.yiren.core.utils.HTTPResult;
import com.yiren.entity.YrCaregiverSendOrdersStandardHistory;
import com.yiren.entity.YrStaffInfo;
import com.yiren.manager.ctrl.GenericController;
import com.yiren.manager.form.pension.YrCaregiverSendOrdersStandardHistoryForm;
import com.yiren.manager.service.iface.department.YrStaffInfoService;
import com.yiren.manager.service.iface.pension.YrCaregiverSendOrdersStandardHistoryService;
import com.yiren.manager.utils.ObjectUtils;

@RequestMapping("caregiverSendOrdersStandardHistory")
@Controller
public class YrCaregiverSendOrdersStandardHistoryController extends GenericController<YrCaregiverSendOrdersStandardHistoryForm, YrCaregiverSendOrdersStandardHistory>{

	@Autowired
	private YrCaregiverSendOrdersStandardHistoryService historyService;
	
	@Autowired
	private YrStaffInfoService yrStaffInfoService;
	
	/**
	 * 根据床位费id查找修改历史列表
	 * @param bedCostId
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("find")
	@ResponseBody
	public HTTPResult<YrCaregiverSendOrdersStandardHistoryForm> find(String caregiverSendOrdersStandardId) throws Exception{
		HTTPResult<YrCaregiverSendOrdersStandardHistoryForm> result = new HTTPResult<YrCaregiverSendOrdersStandardHistoryForm>();
		if(StringUtil.isEmpty(caregiverSendOrdersStandardId)){
			throwExp("param.error");
		}
		String sqlWhere = "caregiver_send_orders_standard_id=?1";
		 
		QueryResult<YrCaregiverSendOrdersStandardHistory> qr = historyService.find(YrCaregiverSendOrdersStandardHistory.class, sqlWhere, new Object[]{caregiverSendOrdersStandardId});
		
		List<YrCaregiverSendOrdersStandardHistoryForm> formList = new ArrayList<YrCaregiverSendOrdersStandardHistoryForm>();
		for(YrCaregiverSendOrdersStandardHistory entity : qr.getResultList()){
			YrCaregiverSendOrdersStandardHistoryForm form = toForm(entity);
			if(null!=entity.getUpdateId()){
				YrStaffInfo staffInfo = yrStaffInfoService.findOne(entity.getUpdateId());
				if(staffInfo!=null){
					form.setUpdateName(staffInfo.getName());
				}
			}
			formList.add(form);
		}	
		result.setData(formList);
		
		return result;
		
	}
	
	@Override
	public YrCaregiverSendOrdersStandardHistoryForm toForm(YrCaregiverSendOrdersStandardHistory e) throws Exception {
		YrCaregiverSendOrdersStandardHistoryForm form = ObjectUtils.objectToObject(e, YrCaregiverSendOrdersStandardHistoryForm.class);
		return form;
	}

	@Override
	public YrCaregiverSendOrdersStandardHistory toEntity(YrCaregiverSendOrdersStandardHistoryForm f) throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

}
